package com.ricohgag.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ricohgag.entity.News;
import com.ricohgag.entity.NewsCatalog;
import com.ricohgag.mapper.NewsCatalogMapper;
import com.ricohgag.mapper.NewsMapper;
import com.ricohgag.service.NewsCatalogService;
import com.ricohgag.util.vo.NewsCatalogVo;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 肖晖
 * @since 2018-08-25
 */
@Service
public class NewsCatalogServiceImpl extends ServiceImpl<NewsCatalogMapper, NewsCatalog> implements NewsCatalogService {
    @Autowired
    private NewsCatalogMapper newsCatalogMapper;
    @Autowired
    private NewsMapper newsMapper;

    /**
     * 分级查询新闻
     * @return
     * @throws Exception
     */
    @Override
    public List<NewsCatalogVo> selectNewsCatalogVo() throws Exception{
        QueryWrapper<NewsCatalog> qw = new QueryWrapper<>();
        qw.eq("catalog_id", 0);
        List<NewsCatalog> newsCatalogs = newsCatalogMapper.selectList(qw);
        List<NewsCatalogVo> newsCatalogVos = new ArrayList<NewsCatalogVo>();

        for(NewsCatalog newsCatalog:newsCatalogs){
            NewsCatalogVo newsCatalogVo = new NewsCatalogVo();
            newsCatalogVo.setNewsCatalog(newsCatalog);
            newsCatalogVo.setNewsCatalogs(newsCatalogMapper.selectList(new QueryWrapper<NewsCatalog>().eq("catalog_id", newsCatalog.getId())));

            newsCatalogVos.add(newsCatalogVo);
        }

        return newsCatalogVos;
    }

    /**
     * 列表查询新闻
     * @return
     * @throws Exception
     */
    @Override
    public IPage<NewsCatalog> selectNewsCatalog(Page<NewsCatalog> page) throws Exception{
        IPage<NewsCatalog> newsCatalogIPage = newsCatalogMapper.selectPage(page, null);
        Assert.assertSame(page, newsCatalogIPage);

        return newsCatalogIPage;

    }

    @Override
    public NewsCatalog selectNewsCatalogOne(Integer id) throws Exception{

        return newsCatalogMapper.selectById(id);
    }

    @Override
    public int updateNewsCatalog(NewsCatalog newsCatalog) throws Exception{
        return newsCatalogMapper.updateById(newsCatalog);
    }

    @Override
    public int insertNewsCatalog(NewsCatalog newsCatalog) throws Exception{
        return newsCatalogMapper.insert(newsCatalog);
    }

    @Override
    public int deleteNewsCatalog(int id) throws Exception{
        //查询此分类下的分类，
        List<NewsCatalog> newsCatalogs = newsCatalogMapper.selectList(new QueryWrapper<NewsCatalog>().eq("catalog_id", id));
        //查询属于此分类的新闻
        List<News> newsList = newsMapper.selectList(new QueryWrapper<News>().eq("catalog_id", id));
        //递归删除此分类下的分类
        for(NewsCatalog newsCatalog:newsCatalogs){
            deleteNewsCatalog(newsCatalog.getId());
        }

        //删除此分类下的新闻
        for(News news:newsList){
            newsMapper.deleteById(news.getId());
        }

        //删除自己
        return newsCatalogMapper.deleteById(id);
    }

}
